package com.adtech.adms.commons.file.web.action;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;

import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;

import com.adtech.adms.commons.file.pojo.FileInfo;
import com.adtech.adms.commons.util.SqlValidate;
import com.adtech.core.Settings;
import com.adtech.core.util.FileUtil;
import com.adtech.core.web.action.AjaxResult;
import com.adtech.core.web.action.BaseAction;

/**
 * 
 * 
 * @author Dormin 
 * @since 2011-6-13 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class UploadingAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private File filedata;
	private String filename;
	private String folderPath;
	private FileInfo fileInfo;
	private String fileext;
	private String folder;

	public String getFolder() {
		return folder;
	}

	public void setFolder(String folder) {
		this.folder = folder;
	}

	public String getFileext() {
		return fileext;
	}

	public void setFileext(String fileext) {
		this.fileext = fileext;
	}

	public String getFolderPath() {
		return folderPath;
	}

	public void setFolderPath(String folderPath) {
		this.folderPath = folderPath;
	}

	public File getFiledata() {
		return filedata;
	}

	public void setFiledata(File filedata) {
		this.filedata = filedata;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public FileInfo getFileInfo() {
		return fileInfo;
	}

	public void setFileInfo(FileInfo fileInfo) {
		this.fileInfo = fileInfo;
	}

	/**
	 * @Function 执行上传
	 * @param
	 * @return
	 * @throws IOException
	 * @exception
	 * 
	 * @author Dormin
	 * @changed
	 * 
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	public String excute() throws IOException {
		
		
		if(!SqlValidate.validate( URLDecoder.decode(filename))){
			return "securityexceptionerror";
		}
		
		if(!SqlValidate.validate( URLDecoder.decode(fileext))){
			return "securityexceptionerror";
		}
		
		if(!SqlValidate.validate(URLDecoder.decode(filedata.toString()))){
			return "securityexceptionerror";
		}
		
		if(!SqlValidate.validate(URLDecoder.decode(folder))){
			return "securityexceptionerror";
		}
		
	
		
		if (filedata.length() == 0) {

			this.fileInfo = new FileInfo();
			return SUCCESS;
		}
		String ext = FileUtil.getFileExtension(filename);// filename.split("\\.")[1];
		
		if(".exe,.dat,.cmd,.dll,.jsp,.asp,.aspx,.php".indexOf(ext.toLowerCase()) >=0){

			this.fileInfo = new FileInfo();
			fileInfo.setRealName(filename);
			fileInfo.setRealPath("nottype");
			return SUCCESS;
		} else {
			if(filename.indexOf("&")>-1) {
				/*this.fileInfo = new FileInfo();
				fileInfo.setRealName(filename);
				fileInfo.setRealPath("invalidchar");
				return SUCCESS;*/
			}
		}

		String path = Settings.get(Settings.TMPDIR_PATH);
		String filerealPath = FileUtil.generateUniqueFileName(path, ext);
		File fileobj = new File(filerealPath);
		FileUtils.copyFile(filedata, fileobj);
		this.fileInfo = new FileInfo();
		fileInfo.setRealName(filename);
		fileInfo.setRealPath(filerealPath);
		java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
		double byteSize = new Double(fileobj.length())/1024;
		if(byteSize<1000) {
			fileInfo.setFileSize(df.format(byteSize)+"KB");
		} else {
			fileInfo.setFileSize(df.format(byteSize/1024)+"MB");
		}
		return SUCCESS;

	}

	private String fileJson;

	public String getFileJson() {
		return fileJson;
	}

	public void setFileJson(String fileJson) {
		this.fileJson = fileJson;
	}

	/**
	 * @Function 删除文件
	 * @param
	 * @return
	 * @exception
	 * 
	 * @author Dormin
	 * @changed
	 * 
	 */
	public String del()  {

		JSONObject jsonObj = JSONObject.fromObject(fileJson);
		String realPath = jsonObj.getString("realPath");
		File file = new File(realPath);
		file.delete();
		setAjaxResult(AjaxResult.success());
		return SUCCESS;
	}
}
